import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'index.dart';

/// 设置页面视图
class SettingsPage extends GetView<SettingsController> {
  const SettingsPage({super.key});
  
  /// 构建设置项
  Widget _buildSettingItem({
    required String iconPath,
    required String title,
    required VoidCallback onTap,
    bool showDivider = true,
  }) {
    return Column(
      children: [
        InkWell(
          onTap: onTap,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
            child: Row(
              children: [
                // 图标
                Image.asset(
                  iconPath,
                  width: 24.r,
                  height: 24.r,
                  fit: BoxFit.contain,
                  errorBuilder: (context, error, stackTrace) {
                    return Icon(
                      Icons.settings_outlined,
                      size: 24.r,
                      color: Colors.black87,
                    );
                  },
                ),
                SizedBox(width: 12.w),
                // 标题
                Expanded(
                  child: Text(
                    title,
                    style: TextStyle(
                      fontSize: 15.sp,
                      color: Colors.black87,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ),
                // 箭头
                Icon(
                  Icons.arrow_forward_ios,
                  size: 16.r,
                  color: Colors.grey[400],
                ),
              ],
            ),
          ),
        ),
        // 分隔线
        if (showDivider)
          Divider(
            height: 1,
            thickness: 0.5,
            indent: 52.w,
            color: Colors.grey[200],
          ),
      ],
    );
  }
  
  /// 构建主视图
  Widget _buildView() {
    return Column(
      children: [
        // 设置列表
        Expanded(
          child: Container(
            color: Colors.white,
            child: ListView(
              children: [
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_account_manage.png',
                  title: '账号与管理',
                  onTap: controller.onAccountManageTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_region.png',
                  title: '所选区域',
                  onTap: controller.onRegionSelectTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_privacy.png',
                  title: '隐私协议',
                  onTap: controller.onPrivacyPolicyTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_user_agreement.png',
                  title: '用户协议',
                  onTap: controller.onUserAgreementTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_about_us.png',
                  title: '关于我们',
                  onTap: controller.onAboutUsTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_data_service.png',
                  title: '数据推广服务协议',
                  onTap: controller.onDataPromotionAgreementTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_ad_service.png',
                  title: '广告运维服务协议',
                  onTap: controller.onAdServiceAgreementTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_switch_account.png',
                  title: '切换账号',
                  onTap: controller.onSwitchAccountTap,
                ),
                _buildSettingItem(
                  iconPath: 'assets/icons/icon_customer_service.png',
                  title: '联系客服',
                  onTap: controller.onContactServiceTap,
                  showDivider: false,
                ),
              ],
            ),
          ),
        ),
        
        // 退出登录按钮
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(16.w),
          color: Colors.white,
          child: SafeArea(
            top: false,
            child: Obx(() => ElevatedButton(
              onPressed: controller.isLoading.value 
                  ? null 
                  : controller.onLogoutTap,
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFFFF4757),
                foregroundColor: Colors.white,
                disabledBackgroundColor: Colors.grey[300],
                elevation: 0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12.r),
                ),
                padding: EdgeInsets.symmetric(vertical: 14.h),
              ),
              child: controller.isLoading.value
                  ? SizedBox(
                      width: 20.r,
                      height: 20.r,
                      child: const CircularProgressIndicator(
                        strokeWidth: 2,
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      ),
                    )
                  : Text(
                      '退出登录',
                      style: TextStyle(
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
            )),
          ),
        ),
      ],
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        title: const Text('设置'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0.5,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () => Get.back(),
        ),
      ),
      body: _buildView(),
    );
  }
}

